package com.zoncont.estate.service.impl;

import com.zoncont.estate.formbean.DeleteMenuFormBean;
import com.zoncont.estate.domain.Menu;
import com.zoncont.estate.domain.Role;
import com.zoncont.estate.domain.User;
import com.zoncont.estate.domain.UserRole;
import com.zoncont.estate.mapper.*;
import com.zoncont.estate.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户 业务层处理
 *
 * @author zoncont.ahanu
 */
@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private SaasMenuMapper menuMapper;
    @Autowired
    private RoleMenuMapper roleMenuMapper;


    /**
     * 新增保存用户信息
     *
     * @param role 权限数据
     * @param user 用户数据
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertUser(Role role, User user) {
        String company = role.getCompany();
        // 新增用户信息
        Long userId = this.insertUser(user);
        // 新增管理角色
        Long roleId = this.insertRole(role);
        // 新增用户与角色管理
        this.insertUserRole(new UserRole(userId, roleId, company));
        // 批量插入菜单
        this.batchMenu(role.getMenus(), role.getMenuIds(), company);
    }

    /**
     * 修改用户密码
     *
     * @param user 用户信息
     * @return 结果
     */
    @Override
    public int resetUserPwd(User user) {
        return userMapper.updateUser(user);
    }

    /**
     * 根据公司ID获取管理员ID
     * @param compId
     * @return
     */
    @Override
    public Long selectUserIdByCompId(String compId){
        return userMapper.selectUserIdByCompId(compId);
    }

    /**
     * 批量新增菜单
     *
     * @param menus   所有菜单
     * @param menuIds 所选菜单ID
     * @param company 公司ID
     * @return
     */
    private void batchMenu(List<Menu> menus, List<Long> menuIds, String company) {
        // 获取公司菜单
        List<Long> menuIdList = menuMapper.selectByCompany(company);
        // 获取取消菜单
        List<Long> removeList = menuIdList.stream().filter(item -> !menuIds.contains(item)).collect(Collectors.toList());
        if(removeList != null && !removeList.isEmpty()){
            // 删除角色与菜单关联
            List<Long> remenuIds = menuMapper.selectIdByCompany(removeList, company);
            DeleteMenuFormBean form = new DeleteMenuFormBean(remenuIds, company);
            roleMenuMapper.deleteRoleMenuByMenuId(form);
            menuMapper.batchDeleteMenuById(form);
        }
        menus = menus.stream().filter(item -> {
            boolean result = false;
            if (menuIds.contains(item.getMenuId()) && (menuIdList == null || !menuIdList.contains(item.getMenuId()))) {
                item.setCompany(company);
                result = true;
            }
            return result;
        }).collect(Collectors.toList());
        if(menus != null && !menus.isEmpty()){
            menuMapper.batchMenu(menus);
        }
    }

    /**
     * 新增用户角色关联
     *
     * @param userRole
     * @return
     */
    private void insertUserRole(UserRole userRole) {
        if (userRoleMapper.selectByQuery(userRole) == null) {
            userRoleMapper.insertUserRole(userRole);
        }
    }

    /**
     * 新增用户
     *
     * @param user
     * @return
     */
    private Long insertUser(User user) {
        Long userId = userMapper.selectUserByLoginName(user.getLoginName(), user.getCompany());
        if(userId == null){
            userMapper.insertUser(user);
            userId = user.getUserId();
        }
        return userId;
    }

    /**
     * 新增角色
     *
     * @param role
     * @return
     */
    private Long insertRole(Role role) {
        Long roleId = roleMapper.selectByRoleKey(role.getRoleKey(), role.getCompany());
        if ( roleId == null ) {
            role.setRoleSort("1");
            role.setStatus("0");
            roleMapper.insertRole(role);
            roleId = role.getRoleId();
        }
        return roleId;
    }

}
